#include "StdAfx.h"
#include "SelectionOperator.h"
#include "QueueEntity.h"
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
SelectionOperator::SelectionOperator(void)
{
	
}


SelectionOperator::~SelectionOperator(void)
{
}


void SelectionOperator::execution(int executionTimes)
{
	
#ifdef DEBUG
	std::cout<<"===================operator begin================="<<std::endl;
	std::cout<<"operatorid : "<<this->getId()<<std::endl;
#endif
	assert(this->getInputQueueList().size()==1);
	assert(this->getOutputQueueList().size()==1);
	boost::shared_ptr<QueueEntity>inputQueue = this->getInputQueueList().front();
	boost::shared_ptr<QueueEntity>outputQueue = this->getOutputQueueList().front();
	for ( int i = 0 ; i < executionTimes ; i++) 
	{
		Element inputElement;
		if(outputQueue->isFull())
		{
			break;
		}
		if(inputQueue->isEmpty())
		{
			break;
		}
		inputQueue->dequeue(inputElement);
		if(isSatisfySelectionCondition(inputElement))
		{
			outputQueue->enqueue(inputElement);
		}

	}
#ifdef DEBUG
	std::cout<<"===================operator over================="<<std::endl;
#endif
}

void SelectionOperator::setSelectionCondition(std::string selectionCondition)
{
	this->selectionCondition = selectionCondition;
}
bool SelectionOperator::isSatisfySelectionCondition(Element& element)
{
	std::string selectionCondition = this->selectionCondition;
	if (std::string::npos != selectionCondition.find("like"))
	{ //use regular expression 
		//wangyan need change 
		std::string regularString = selectionCondition.substr(selectionCondition.find_last_of("like")+1,selectionCondition.size());
		boost::regex expression(regularString);
		std::string title = element.document.getField("title").valuestr();
		boost::cmatch what;
		bool isMatch = regex_search(title.c_str(), what, expression);
		
		return isMatch;
	}
	else if (std::string::npos != selectionCondition.find("<>"))
	{ 
		//not equal
		std::string attributeName = selectionCondition.substr(0,selectionCondition.find("<>"));
		boost::trim(attributeName);
		std::string value = selectionCondition.substr(selectionCondition.find("<>")+std::string("<>").length());
		boost::trim(value);
		//assert(value[0]=='\'');
		//assert(value[value.length()-1]=='\'');
		//std::cout<<element.document<<std::endl;
		if(value=="null")
		{
			if(element.document.getField(attributeName).isNull()==true)
			{
				return false;
			}
			else
			{
				std::cout<<element.document<<std::endl;
			}
		}
		
	
	}
	else if (std::string::npos != selectionCondition.find("personid"))
	{//wangyan currently for people flow, need change
		//std::cout<<element.document<<std::endl;
		std::string max = selectionCondition.substr(0,selectionCondition.find(">")); 
		std::string min = selectionCondition.substr(selectionCondition.find_last_of(">")+1); 
		int valueint;
		element.document.getField("personid").Val(valueint);
		int maxint = boost::lexical_cast<int>(max);
		int minint = boost::lexical_cast<int>(min);
		
		if(valueint > minint && valueint<maxint)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	return true;
}